﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.InteropServices;

namespace An_Im.AddInConsole
{
    public class Program
    {
        static void Main(string[] args)
        {
            PPT.Application app = Marshal.GetActiveObject("PowerPoint.Application") as PPT.Application;
            Marco(app);
            //Console.ReadKey();
        }

        public static void Marco(PPT.Application app)
        {
           
        }

        /// <summary>
        /// 按条件选中图形。
        /// </summary>
        /// <param name="app"></param>
        /// <param name="predicate"></param>
        public static void SelectShapeByPredicate(PPT.Application app, Predicate<Tuple<PPT.Shape, PPT.Shape>> predicate)
        {
            var pres = app.ActivePresentation;
            var slideRange = GetCurrentSlideRange(app);
            var shapeRange = GetCurrentShapeRange(app);
            if (slideRange == null) return;
            if (shapeRange == null) return;

            var slide = slideRange[1];
            var shape = shapeRange[1];
            var shapes = slide.Shapes;

            shape.Select(Microsoft.Office.Core.MsoTriState.msoTrue);

            foreach (PPT.Shape shp in shapes)
            {
                if (predicate(new Tuple<PPT.Shape, PPT.Shape>(shape, shp))) shp.Select(Microsoft.Office.Core.MsoTriState.msoFalse);
            }
        }


        /// <summary>
        /// 获得演示文稿的宽度。
        /// </summary>
        /// <param name="pres">演示文稿</param>
        /// <returns></returns>
        public static float GetPresentationWidth(PPT.Presentation pres)
        {
            return pres.PageSetup.SlideWidth;
        }

        /// <summary>
        /// 获得演示文稿的高度。
        /// </summary>
        /// <param name="pres">演示文稿</param>
        /// <returns></returns>
        public static float GetPresentationHeight(PPT.Presentation pres)
        {
            return pres.PageSetup.SlideHeight;
        }

        public static PPT.ShapeRange GetCurrentShapeRange(PPT.Application app)
        {
            var selection = app.ActiveWindow.Selection;
            if (selection.Type == PPT.PpSelectionType.ppSelectionShapes) return selection.ShapeRange;
            return null;
        }

        public static PPT.SlideRange GetCurrentSlideRange(PPT.Application app)
        {
            var selection = app.ActiveWindow.Selection;
            try
            {
                return selection.SlideRange;
            }
            catch
            {
                return null;
            }
        }


    }
}
